﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge;
using AForge.Video;
using AForge.Video.DirectShow;

namespace CarAutomation
{
    public partial class WebcamSelection : Form
    {
        MainForm parentForm;
        public WebcamSelection(MainForm parent)
        {
            InitializeComponent();
            parentForm = parent;
            LoadAvaliableWebcams();
            if (webcamComboBox.Items.Count == 0) { startButton.Enabled = false; }
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            parentForm.finalVideoSource = new VideoCaptureDevice(
                        videoCaptureDevices[webcamComboBox.SelectedIndex].MonikerString);
            this.Close();
            parentForm.UpdateWebcamSettings();
        }

        private FilterInfoCollection videoCaptureDevices;
        public VideoCaptureDevice finalVideoSource = new VideoCaptureDevice();

        private void LoadAvaliableWebcams()
        {
            //find all avaliable webcams
            videoCaptureDevices = new FilterInfoCollection(
                FilterCategory.VideoInputDevice);

            foreach (FilterInfo videoCaptureDevice in videoCaptureDevices)
            {
                //add all webcams to the drop down list
                webcamComboBox.Items.Add(videoCaptureDevice.Name);
            }
            if (webcamComboBox.Items.Count > 0) { webcamComboBox.SelectedIndex = 0; }
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }
    }
}
